----------------------------------------------------------------------------------------------------------------------------------
--[[

    技能控制器

]]--
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_spell_controller = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}
    -------------------------------------------------------
    -- classified
        self.AttachClassified = function(self,classified)
            self.classified = classified            
        end
        self.classified = SpawnPrefab("loramia_other_spells_classified")
        self.classified:LinkPlayer(self.inst,self)
    -------------------------------------------------------
    --- timers 计时器
        self.timers = {}
        self:AddOnSaveFn(function()
            self:Set("timers",self.timers)
        end)
        self:AddOnLoadFn(function()
            self.timers = self:Get("timers") or {}
            self:TimerInit()
        end)
    -------------------------------------------------------
end,
nil,
{

})
------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function loramia_com_spell_controller:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function loramia_com_spell_controller:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function loramia_com_spell_controller:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function loramia_com_spell_controller:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function loramia_com_spell_controller:Get(index)
        if index then
            return self.DataTable[index]
        end
        return nil
    end
    function loramia_com_spell_controller:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function loramia_com_spell_controller:Add(index,num)
        if index then
            self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
            return self.DataTable[index]
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------
---- 计时器
    function loramia_com_spell_controller:TimerInit()
        if self.___timers_task == nil then
            self.___timers_task = self.inst:DoPeriodicTask(0.1,function()
                local need_2_stop_timer_flag = 0
                for index, num in pairs(self.timers) do
                    self.timers[index] = math.max(num - 0.1 , 0)
                    need_2_stop_timer_flag = need_2_stop_timer_flag + self.timers[index]
                    self:NetSetData(index,self.timers[index])
                end
                if need_2_stop_timer_flag == 0 then
                    self.___timers_task:Cancel()
                    self.___timers_task = nil
                end
            end)
        end
    end
    function loramia_com_spell_controller:StartTimer(index,time)
        self.timers[index] = time
        self:TimerInit()
    end
    function loramia_com_spell_controller:IsReady(index)
        return self.timers[index] == 0 or self.timers[index] == nil
    end
------------------------------------------------------------------------------------------------------------------------------
---- 设置参数
    function loramia_com_spell_controller:NetSetData(index,value)
        self.classified:Set(index,value)
    end
    function loramia_com_spell_controller:NetGetData(index,default)
        return self.classified:Get(index) or default
    end
------------------------------------------------------------------------------------------------------------------------------
    function loramia_com_spell_controller:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function loramia_com_spell_controller:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_spell_controller







